package com.fitzone.web.controller.mini;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitzone.common.context.UserContext;
import com.fitzone.common.exception.BaseException;
import com.fitzone.common.result.Result;
import com.fitzone.model.dto.*;
import com.fitzone.model.entity.Orders;
import com.fitzone.model.entity.User;
import com.fitzone.model.vo.*;
import com.fitzone.web.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mini/order")
@RequiredArgsConstructor
@Api(tags = "小程序订单相关接口")
public class MiniOrderController {
    private final OrdersService ordersService;
    @ApiOperation("预约课程")
    @PostMapping("/course")
    public Result appointmentCourse(@RequestBody OrdersCourseDTO ordersCourseDTO){
        ordersService.orderCourse(ordersCourseDTO);
        return Result.ok();
    }

    @ApiOperation("预约教练")
    @PostMapping("/coach")
    public Result appointmentCoach(@RequestBody OrdersCoachDTO ordersCourseDTO){
        ordersService.orderCoach(ordersCourseDTO);
        return Result.ok();
    }

    @ApiOperation("购买会员")
    @PostMapping("/vip")
    public Result buyVip(@RequestBody OrdersVipDTO ordersVipDTO){
        ordersService.orderVip(ordersVipDTO);
        return Result.ok();
    }

    @ApiOperation("取消预约课程")
    @DeleteMapping("/appointCourse/{id}")
    public Result cancelAppointmentCourse(@PathVariable Long id){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,id);
        List<Orders> list = ordersService.list(queryWrapper);
        for (Orders orders : list) {
            if (orders.getStatus() >= 2){
                throw new BaseException("课程已经开始，不能取消预约");
            }
            ordersService.remove(queryWrapper);
        }
        return Result.ok();
    }
    @ApiOperation("取消预约教练")
    @DeleteMapping("/appointCoach/{id}")
    public Result cancelAppointmentCoach(@PathVariable Long id){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,id);
        ordersService.remove(queryWrapper);
        return Result.ok();
    }

    @GetMapping("/courseOrder")
    @ApiOperation("查询预约课程订单")
    public Result<List<MiniOrdersCourseVO>> page(@RequestParam Integer status){
        Long currentId = UserContext.getCurrentId();
        List<MiniOrdersCourseVO> courseVOList = ordersService.getCourse(currentId,status);
        return Result.ok(courseVOList);
    }

    @GetMapping("/vipOrder")
    @ApiOperation("查询预约会员订单")
    public Result<List<MiniOrdersVipVO>> pageVip(@RequestParam Integer status){
        Long currentId = UserContext.getCurrentId();
        List<MiniOrdersVipVO> vipVOList = ordersService.getVip(currentId,status);
        return Result.ok(vipVOList);
    }

    @GetMapping("/coachOrder")
    @ApiOperation("查询预约教练订单")
    public Result<List<MiniOrdersCoachVO>> pageCoach(@RequestParam Integer status){
        Long currentId = UserContext.getCurrentId();
        List<MiniOrdersCoachVO> coachVOList = ordersService.getCoach(currentId,status);
        return Result.ok(coachVOList);
    }

    @GetMapping("/ordersCourse/{oid}/{status}")
    @ApiOperation("根据id和状态查询课程预约详情")
    public Result getOrdersCourseById(@PathVariable Long oid,@PathVariable Integer status){
        OrdersCourseVO ordersCourseVO = ordersService.getCourseById(oid,status);
        return Result.ok(ordersCourseVO);
    }

    @GetMapping("/ordersCoach/{oid}/{status}")
    @ApiOperation("根据id和状态查询教练预约详情")
    public Result getOrdersCoachById(@PathVariable Long oid,@PathVariable Integer status){
        OrdersCoachVO ordersCoachVO = ordersService.getCoachById(oid,status);
        return Result.ok(ordersCoachVO);
    }

    @GetMapping("/ordersVip/{oid}/{status}")
    @ApiOperation("根据id和状态查询会员订单详情")
    public Result getOrdersVipById(@PathVariable Long oid,@PathVariable Integer status){
        OrdersVipVO ordersVipVO = ordersService.getVipById(oid,status);
        return Result.ok(ordersVipVO);
    }
}
